﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Data;

namespace workshop3
{
    public static class ClassCreateXML
    {
        public static void CreateAccountXmlFile(String name, String pwd)//first create the account file
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);
            XmlNode root = xmlDoc.CreateElement("Account");
            xmlDoc.AppendChild(root);
            XmlElement username = xmlDoc.CreateElement("username");
            XmlElement password = xmlDoc.CreateElement("password");
            root.AppendChild(username);
            root.AppendChild(password);
            username.InnerText = name;
            password.InnerText = pwd;
            xmlDoc.Save("Account.xml");
        }
        public static int checktheaccount()//check if the account file exist
        {
            if (File.Exists("Account.xml")){
                return 1;
            }
            else return 0;
        }
        public static void CreateXmlFile(String filename)//create the xml file 
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmldoc.AppendChild(node);
            xmldoc.Save(filename+".xml");
        }
        public static bool AddCourseXml(string name,string year,string semester, DataTable dt,DataTable dt2)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("course.xml");
            String path = "//courses[@number]";
            XmlNode node1 = xmldoc.SelectSingleNode(path);
            int temp = 0;
            int num=0;
            int.TryParse(node1.Attributes[0].Value, out num);//extract the total number of the course existed in the xml
            temp = num;//assign the total number to the variable

            String path1 = "//courses";
            XmlNode root = xmldoc.SelectSingleNode(path1);
            XmlElement course = xmldoc.CreateElement("course");
            course.Attributes.Append(CreateAttribute(course, "id",temp.ToString()));
            root.AppendChild(course);

            XmlElement coursename = xmldoc.CreateElement("coursename");
            coursename.InnerText = name;
            course.AppendChild(coursename);

            XmlElement courseyear = xmldoc.CreateElement("year");
            courseyear.InnerText = year;
            course.AppendChild(courseyear);

            XmlElement coursesemester = xmldoc.CreateElement("semester");
            coursesemester.InnerText = semester;
            course.AppendChild(coursesemester);

            XmlElement ams = xmldoc.CreateElement("AMS");
            ams.Attributes.Append(CreateAttribute(ams, "id", num.ToString()));
            for(int i = 0; i < dt2.Rows.Count; i++)
            {
                XmlElement amsitem = xmldoc.CreateElement("item");
                amsitem.Attributes.Append(CreateAttribute(amsitem, "id", i.ToString()));
                amsitem.Attributes.Append(CreateAttribute(amsitem, "name", dt2.Rows[i][0].ToString()));
                amsitem.InnerText = dt2.Rows[i][1].ToString();
                ams.AppendChild(amsitem);
            }
            course.AppendChild(ams);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //ClassCreateXML.AddTask(node, dt.Rows[i][0].ToString(), i);
                XmlElement task = xmldoc.CreateElement("task");
                task.Attributes.Append(CreateAttribute(task, "id", i.ToString()));
                task.InnerText = dt.Rows[i][0].ToString();
                course.AppendChild(task);
            }

            temp++;//add 1 to the total number 
            node1.Attributes[0].Value = temp.ToString();
            xmldoc.Save("course.xml");
            XmlDocument check = new XmlDocument();
            check.Load("course.xml");
            XmlNodeList checklist = check.SelectNodes("//course");
            for (int i = 0; i < checklist.Count; i++)
            {
                if (checklist[i].Attributes[0].Value == num.ToString())
                {
                    return true;
                }
            }
            return false;
        }

        public static XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;       //createa new xmldocument class which is our own document   
                XmlAttribute attr = doc.CreateAttribute(attributeName);
                attr.Value = value;     //input the value in it
                node.Attributes.SetNamedItem(attr);
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }
        public static bool deletexml(string filename,string path,string id)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(filename);
            XmlNodeList nodeList = xml.SelectNodes(path);
            int temp=0;
            int.TryParse(nodeList[0].ParentNode.Attributes[0].Value, out temp);
            temp = temp - 1;
            nodeList[0].ParentNode.Attributes[0].Value = temp.ToString();
            for(int i = 0; i < nodeList.Count; i++)//find the corresponding node in the xml
            {
                if (nodeList[i].Attributes[0].Value == id)
                {
                    nodeList[i].ParentNode.RemoveChild(nodeList[i]);//remove the selected node 
                }
            }
            xml.Save(filename);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filename);
            XmlNodeList nodeListcheck = xmldoc.SelectNodes(path);

            for(int j = 0; j < nodeListcheck.Count; j++)//check if the node existed in the xml or not
            {
                if (nodeListcheck[j].Attributes[0].Value == id)
                {
                    return false;//if find, return false, means that the node is not successfully deleted
                }
            }
            return true;
        }
        public static bool AddRubricXml(DataTable dt, string name, string courseid)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load("rubric.xml");
            string path = "//rubrics[@number]";
            XmlNode root = xml.SelectSingleNode(path);
            int temp=0;
            int id = 0;
            int.TryParse(root.Attributes[0].Value, out temp);
            id = temp;

            XmlElement rubric = xml.CreateElement("rubric");
            rubric.Attributes.Append(CreateAttribute(rubric, "id", id.ToString()));
            rubric.Attributes.Append(CreateAttribute(rubric, "name", name));
            rubric.Attributes.Append(CreateAttribute(rubric, "courseid", courseid));
            
            root.AppendChild(rubric);

            for(int i = 0; i < dt.Rows.Count; i++)
            {
                XmlElement item = xml.CreateElement("item");
                item.Attributes.Append(CreateAttribute(item, "id", i.ToString()));
                item.Attributes.Append(CreateAttribute(item, "name", dt.Rows[i][0].ToString()));
                item.InnerText = dt.Rows[i][1].ToString();
                rubric.AppendChild(item);
            }


            temp++;
            root.Attributes[0].Value = temp.ToString();
            xml.Save("rubric.xml");
            XmlDocument check = new XmlDocument();
            check.Load("rubric.xml");
            XmlNodeList checklist = check.SelectNodes("//rubric");
            for(int i=0; i < checklist.Count; i++)//check if the corresponding rubric is successfully add or not
            {
                if (checklist[i].Attributes[0].Value == id.ToString())
                {
                    return true;
                }
            }
            return false;
        }

        public static bool TextCheck(string input)//test if the input is string or not, if yes return true
        {
            int temp;
            if(!int.TryParse(input, out temp))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool NegativeCheck(int input)//if negative return false
        {
            if (input < 0) return false;
            else return true;
        }

        public static bool NumCheck(int input, int downrange, int uprange)//check the input is within the range the user set
        {
            if (input < downrange) return false;
            if (input > uprange) return false;
            else return true;
        }

        public static XmlNodeList loadxmldata(string courseid, string path,string filename,string where)//load the data from the selected xml 
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(filename);
            XmlNodeList nodelist = xml.SelectNodes(path);
            for(int i=0; i < nodelist.Count; i++)
            {
                if (nodelist[i].Attributes[0].Value == courseid)
                {
                    if (where == "AMS")
                    {
                        XmlNodeList returnchild = nodelist[i].ChildNodes[3].ChildNodes;
                        return returnchild;
                    }
                    if(where == "task")
                    {

                        XmlNodeList returnchild=nodelist[i].ChildNodes;
                        return returnchild;
                    }
                    if(where == "studentfile")
                    {
                        XmlNodeList returnchild = nodelist[i].ChildNodes;
                        return returnchild;
                    }
                }
            }
            
            return null;
        }
    }
}
